package com.mediacloud.app.newsmodule.view;


import com.bumptech.glide.Glide;
import com.mediacloud.app.assembly.photoview.IPhotoView;
import com.mediacloud.app.assembly.photoview.PhotoViewAttacher;
import com.mediacloud.app.assembly.photoview.PhotoViewAttacher.OnViewTapListener;
import com.mediacloud.app.newsmodule.R;

import android.content.Context;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class PictureView extends AppCompatImageView implements IPhotoView {
	protected Context context;
	public  PhotoViewAttacher mAttacher;
	protected ScaleType mPendingScaleType;

	public PictureView(Context context)
	{
		this(context,null);
		this.context = context;
		super.setScaleType(ScaleType.MATRIX);
	}

//	@Override
//	public void setDefaultRes() {
//		loadingScaleType=ScaleType.MATRIX;
//		super.setDefaultRes();
//	}
	public PictureView(Context context, AttributeSet attr) {
		this(context, attr, 0);
		this.context = context;
		super.setScaleType(ScaleType.MATRIX);
	}

	public PictureView(Context context, AttributeSet attr, int defStyle) {
		super(context, attr, defStyle);
		super.setScaleType(ScaleType.MATRIX);
		setBackgroundColor(getResources().getColor(R.color.transparent));
		setAdjustViewBounds(true);
		this.context = context;
		initAttacher();
		if (null != mPendingScaleType) {
			setScaleType(mPendingScaleType);
			mPendingScaleType = null;
		}
//		setImageResource(R.drawable.cctv7_loading_img);
	}

	protected void initAttacher()
	{
		mAttacher = new PhotoViewAttacher4AppFactory(this);
	}



	@Override
	public boolean canZoom() {
		return mAttacher.canZoom();
	}

	@Override
	public RectF getDisplayRect() {
		return mAttacher.getDisplayRect();
	}

	@Override
	public float getMinScale() {
		return mAttacher.getMinScale();
	}

	@Override
	public float getMidScale() {
		return mAttacher.getMidScale();
	}

	@Override
	public float getMaxScale() {
		return mAttacher.getMaxScale();
	}

	@Override
	public float getScale() {
		return mAttacher.getScale();
	}

	@Override
	public ScaleType getScaleType() {
		return mAttacher.getScaleType();
	}

	@Override
	public void setAllowParentInterceptOnEdge(boolean allow) {
		mAttacher.setAllowParentInterceptOnEdge(allow);
	}

	@Override
	public void setMinScale(float minScale) {
		mAttacher.setMinScale(minScale);
	}

	@Override
	public void setMidScale(float midScale) {
		mAttacher.setMidScale(midScale);
	}

	@Override
	public void setMaxScale(float maxScale) {
		mAttacher.setMaxScale(maxScale);
	}

	@Override
	// setImageBitmap calls through to this method
	public void setImageDrawable(Drawable drawable) {
		super.setImageDrawable(drawable);
		if (null != mAttacher) {
			mAttacher.update();
		}
	}

	public void setImageDrawable2(Drawable drawable) {
		super.setImageDrawable(drawable);
	}

	@Override
	public void setImageResource(int resId) {
		super.setImageResource(resId);
		if (null != mAttacher) {
			mAttacher.update();
		}
	}

	@Override
	public void setImageURI(Uri uri) {
		super.setImageURI(uri);
		if (null != mAttacher) {
			mAttacher.update();
		}
	}

	public void setImageURL(final String urlStr) {
//		ImageLoader.getInstance().loadImage(urlStr, new ImageLoadingListener() {
//			public void onLoadingStarted(String arg0, View arg1) {
//			}
//			
//			public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
//			}
//			public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
//				if(arg2==null)
//					return;
//				PhotoViewCache cache = new PhotoViewCache();
//				cache.setUrl(urlStr);
//				cache.setBitmap(arg2);
//				setImageBitmap(arg2);
//			}
//			public void onLoadingCancelled(String arg0, View arg1) {
//			}
//		});
//		load(urlStr);
		Glide.with(this)
				.load(urlStr) // 图片地址
				.into(this);
	}

	@Override
	public void setOnMatrixChangeListener(PhotoViewAttacher.OnMatrixChangedListener listener) {
		mAttacher.setOnMatrixChangeListener(listener);
	}

	@Override
	public void setOnLongClickListener(OnLongClickListener l) {
		mAttacher.setOnLongClickListener(l);
	}

	@Override
	public void setOnPhotoTapListener(PhotoViewAttacher.OnPhotoTapListener listener) {
		mAttacher.setOnPhotoTapListener(listener);
	}

	@Override
	public void setOnViewTapListener(OnViewTapListener listener) {
		mAttacher.setOnViewTapListener(listener);
	}

	@Override
	public void setScaleType(ScaleType scaleType) {
		if (null != mAttacher) {
			mAttacher.setScaleType(scaleType);
		} else {
			mPendingScaleType = scaleType;
		}
	}

	@Override
	public void setZoomable(boolean zoomable) {
		mAttacher.setZoomable(zoomable);
	}

	@Override
	public void zoomTo(float scale, float focalX, float focalY) {
		mAttacher.zoomTo(scale, focalX, focalY);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		if(Build.VERSION.SDK_INT>=24)
		{
			return;
		}
		mAttacher.cleanup();
	}

	public void destroy()
	{
	}

}